﻿using System;
using MetroContractSample.Data;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Storage.BulkAccess;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;

// The Item Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234232

namespace MetroContractSample
{
  /// <summary>
  /// A page that displays details for a single item within a group while allowing gestures to
  /// flip through other items belonging to the same group.
  /// </summary>
  public sealed partial class ItemDetailPage : MetroContractSample.Common.LayoutAwarePage
  {

    public ItemDetailPage()
    {
      this.InitializeComponent();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property provides the initial item to be displayed.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      var item = (SampleDataItem)e.Parameter;
      this.DefaultViewModel["Group"] = item.Group;
      this.DefaultViewModel["Items"] = item.Group.Items;
      this.flipView.SelectedItem = item;

      AddDataRequestedHandler();
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
      RemoveDataRequestedHandler();

      base.OnNavigatedFrom(e);
    }



    private async void image_Tapped_1(object sender, TappedRoutedEventArgs e)
    {
      var item = this.flipView.SelectedItem as SampleDataItem;
      await Launcher.LaunchFileAsync(item.FileInfo);
    }



    // How to share files 
    // http://msdn.microsoft.com/ja-jp/library/windows/apps/hh758308.aspx

    private void AddDataRequestedHandler()
    {
      DataTransferManager.GetForCurrentView().DataRequested
          += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.DataRequested);
    }

    private void RemoveDataRequestedHandler()
    {
      DataTransferManager.GetForCurrentView().DataRequested
          -= new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.DataRequested);
    }

    private void ShareButton_Click_1(object sender, RoutedEventArgs e)
    {
      DataTransferManager.ShowShareUI();
    }

    private void DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    {
      var item = this.flipView.SelectedItem as SampleDataItem;

      var req = args.Request;

      req.Data.SetStorageItems(new FileInformation[] { item.FileInfo, });
      req.Data.Properties.Title = "MetroContractSample";
      req.Data.Properties.Description = "選択したファイルを共有コントラクトに送ります";
    }

    private async void LaunchButton_Click_1(object sender, RoutedEventArgs e)
    {
      var item = this.flipView.SelectedItem as SampleDataItem;
      await Launcher.LaunchFileAsync(item.FileInfo);
    }
  }
}
